﻿using ExcelConfig;
using ExcelConfig.StoreAPI;
using syno_exec.csm.cmd;
using System.Text;

namespace syno_exec
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;
            if (string.IsNullOrEmpty(AppConfig.Text("验证密码")))
            {
                Console.Write("press you config password: ");
                var pwd = Console.ReadLine();
                if (pwd != null && (pwd = pwd.Trim()) != string.Empty) AppConfig.WriteText("验证密码", pwd);
            }

            var all_param = new Queue<string>(args.Where(t => !string.IsNullOrWhiteSpace(t)));

            if (all_param.Count == 0)
            {
                Console.WriteLine("没有读取到指令");
                return;
            }
            var cmd = all_param.Dequeue();

            switch (cmd)
            {
                case "info":
                    var info_tasks = ActiveBackupCmd.ReadTasks().Result;

                    foreach (var info in info_tasks)
                    {
                        Console.WriteLine($"发现任务 {info.TaskId}: {info.TaskName}");
                    }

                    break;
                case "bak":

                    var tasks = ActiveBackupCmd.ReadTasks().Result;

                    var taskids = new Queue<int>();
                    while (all_param.Count > 0)
                    {
                        if (int.TryParse(all_param.Dequeue(), out var taskid))
                        {
                            var info = tasks.FirstOrDefault(t => t.TaskId == taskid);
                            if (info != null)
                            {
                                taskids.Enqueue(taskid);
                                Console.WriteLine($"读取到版本备份任务 {taskid}: {info.TaskName}");
                            }
                        }
                    }

                    if (taskids.Count == 0) Console.WriteLine("版本备份任务执行失败，没有读取到任务标识。");
                    else
                    {
                        var ok = ActiveBackupCmd.StartTask(taskids.ToArray()).Result;
                        if (ok) Console.WriteLine("触发版本备份任务成功");
                        else Console.WriteLine("触发版本备份任务失败");
                    }
                    break;
            }
        }
    }
}
